/*
 * Ext JS Library 2.0.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.air.MainWindow = Ext.extend(Ext.Panel, {
	baseCls:'x-air x-window',
	frame:true,
	closable : true,
    minimizable : true,
    maximizable : true,
	resizable:true,
	cls:'x-air-window',
	bufferResize:true,
	
    initComponent : function(){
		this.el = Ext.get(document.body);
        this.el.setHeight = Ext.emptyFn;
        this.el.setWidth = Ext.emptyFn;
        this.el.setSize = Ext.emptyFn;
        this.el.setStyle({
            overflow:'hidden',
            margin:'0',
            border:'0 none'
        });
		this.allowDomMove = false;
        Ext.EventManager.onWindowResize(this.fireResize, this);
		
		Ext.air.MainWindow.superclass.initComponent.call(this);
		
		this.addEvents({
            maximize : true,
            minimize: true,
            restore : true,
            beforeclose : true
        });
		
        //this.renderTo = this.el;
	},
	
	fireResize : function(w, h){
		this.setSize(w, h);
    },
	
	initEvents : function(){
		if(this.minimizable){
            this.addTool({
                id: 'minimize',
                handler: this.minimize,
				scope: this
            });
        }
        if(this.maximizable){
            this.addTool({
                id: 'maximize',
                handler: this.maximize,
				scope: this
            });
            this.addTool({
                id: 'restore',
                handler: this.restore,
				scope: this,
                hidden:true
            });
        }
        if(this.closable){
            this.addTool({
                id: 'close',
                handler: this.close,
				scope: this
            });
        }
		this.header.dom.style.cursor = 'move';
		this.header.on('mousedown', function(e){ 
			if(!e.getTarget( '.x-tool', 1)){
				window.htmlControl.stage.window.startMove(); 
			}
		});
		
		if(this.resizable){
            var tpl = Ext.DomHelper.createTemplate(
	            {cls: "x-air x-resizable-handle x-resizable-handle-{0}"}
	        );
	        tpl.compile();
			
	        // map native handles to Ext generated handles
			Ext.each([{x:'south',n:'BOTTOM'}, {x:'east',n:'RIGHT'}, 
					  {x:'west',n:'LEFT'}, {x:'southeast',n:'BOTTOM_RIGHT'}, 
					  {x:'southwest',n:'BOTTOM_LEFT'}], function(p){
					  	
				var handle = tpl.append(this.el, [p.x]);
				handle.style.cursor = 'move';
				Ext.fly(handle).on('mousedown', function(e) {
					e.stopEvent();
					window.htmlControl.stage.window.startResize( air.NativeWindowResize[p.n] ); 
				});
			}, this);
        }
	},
	
	minimize : function(){
        window.htmlControl.stage.window.minimize();
    	this.fireEvent('minimize', this);
    },

    close : function(){
        if(this.fireEvent("beforeclose", this) !== false){
            window.htmlControl.stage.window.close();
        }
    },

    maximize : function(){
        window.htmlControl.stage.window.maximize();
		this.tools.maximize.hide();
        this.tools.restore.show();
    	this.fireEvent('maximize', this);
    },

    restore : function(){
        window.htmlControl.stage.window.restore();
		this.tools.maximize.show();
        this.tools.restore.hide();
    	this.fireEvent('restore', this);
    	
    }
});